package com.kgc.novel.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookUserMapper;
import com.kgc.novel.domain.entity.BookUser;
import com.kgc.novel.domain.param.BookUserParam;
import com.kgc.novel.service.user.BookUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class BookUserServiceImpl extends ServiceImpl<BookUserMapper, BookUser> implements BookUserService {

    @Resource
    private BookUserMapper userMapper;

    /**
     * 根据条件分页显示会员列表
     *
     * @param param
     * @return
     */
    @Override
    public Res getUserByPage(BookUserParam param) {
        LambdaQueryWrapper<BookUser> lqw = new LambdaQueryWrapper();
        lqw.like(StrUtil.isNotBlank(param.getNickName()), BookUser::getNickName, param.getNickName());
        lqw.eq(ObjUtil.isNotNull(param.getStatus()) && param.getStatus() != -1, BookUser::getStatus, param.getStatus());
        if (param.getPageNo() != null && param.getPageSize() != null) {
            Page<BookUser> page = null;
            if (param.getPageSize() > 50) {
                page = new Page<>(param.getPageNo(), 50);
            } else {
                page = new Page<>(param.getPageNo(), param.getPageSize());
            }
            Page<BookUser> userPage = userMapper.selectPage(page, lqw);
            return Res.returnData(userPage.getRecords(), userPage.getTotal());
        } else {
            return Res.returnData(userMapper.selectList(lqw));
        }
    }

}
